package dao.impl;

import bean.Contact;
import bean.Group;
import dao.ContactDAO;
import dao.GroupDAO;
import javax.naming.NamingException;

import org.eclipse.jdt.annotation.NonNull;

import java.sql.*;
import java.util.List;
import java.util.Vector;

public class GroupDAOImpl implements GroupDAO {
    private Connection conn;

    public GroupDAOImpl(@NonNull Connection conn) {
        this.conn = conn;
    }

    @Override
    public List<Group> select(int accountId) throws SQLException, NamingException {
        List<Group> groups = new Vector<>();
        String sql = "SELECT GROUP_ID,GROUP_NAME FROM GROUPS WHERE ACCOUNT_ID=" + accountId;
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery(sql);
        while (rs.next()) {
            Group temporary = new Group();
            temporary.setGroupId(rs.getInt("GROUP_ID"));
            temporary.setGroupName(rs.getString("GROUP_NAME"));
            groups.add(temporary);
        }
        rs.close();
        s.close();
        return groups;
    }

    @Override
    public Group selectById(@NonNull Group temporary) throws SQLException, NamingException {
        String sql = "SELECT GROUP_NAME FROM GROUPS WHERE GROUP_ID=? AND ACCOUNT_ID=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, temporary.getGroupId());
        ps.setInt(2, temporary.getAccountId());
        ResultSet rs = ps.executeQuery();
        Group group = null;
        if (rs.next()) {
            group = new Group(temporary.getGroupId(), rs.getString(1));
        }
        rs.close();
        ps.close();
        return group;
    }

    @Override
    public boolean insert(@NonNull Group temporary) throws SQLException, NamingException {
        String sql = "INSERT INTO GROUPS VALUES(NULL,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, temporary.getGroupName());
        ps.setInt(2, temporary.getAccountId());
        boolean state = ps.executeUpdate() == 1;
        ps.close();
        return state;
    }

    @Override
    public boolean updateById(@NonNull Group temporary) throws SQLException, NamingException {
        boolean state = false;
        String sql = "UPDATE GROUPS SET GROUP_NAME=? WHERE GROUP_ID=? AND ACCOUNT_ID=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, temporary.getGroupName());
        ps.setInt(2, temporary.getGroupId());
        ps.setInt(3, temporary.getAccountId());
        state = ps.executeUpdate() == 1;
        ps.close();
        return state;
    }

    @Override
    public boolean deleteById(@NonNull ContactDAO contactDAO, @NonNull Group temporary) throws SQLException, NamingException {
        Contact temporaryContact = new Contact(temporary.getGroupId(), temporary.getAccountId());
        // 删除指定组别前，先验证当前账户id下的该组别id是否已被联系人关联
        if (contactDAO.selectIdByGroupId(temporaryContact)) {
            return false;
        }
        String sql = "DELETE FROM GROUPS WHERE GROUP_ID=? AND ACCOUNT_ID=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, temporary.getGroupId());
        ps.setInt(2, temporary.getAccountId());
        boolean state = ps.executeUpdate() == 1;
        ps.close();
        return state;
    }
}
